#include "utils.h"

result_t wait_condition_var(pthread_mutex_t *context_lock,
										  pthread_cond_t *cond_var,
										  uint *num_waiting,
										  bool unlock_mutex)

{
	result_t result = OPERATION_SUCCESS;

	// mark that there is another thread that waits
	if ( NULL != num_waiting )
	{
		++(*num_waiting);
	}

	if ( pthread_cond_wait ( cond_var, context_lock ) )
	{
		ERR_PRINT("pthread_cond_wait() failed!\n");
		result =  ERROR_SYSTEM_CALL;
	}

	// mark that the thread woke up
	if ( NULL != num_waiting)
	{
		--(*num_waiting);
	}

	if ( unlock_mutex )
	{
		CONTEXT_UNLOCK( *context_lock );
	}

	return result;
}

